package org.itboys.core.controller.web.tray;

import com.wordnik.swagger.annotations.Api;
import org.itboys.core.entity.mysql.tray.SundayTrayModel;
import org.itboys.core.service.mysql.web.SundayTrayModelService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：zzq
 * 时间：2018/12/4
 * 描述：托盘模板
 */
@Controller
@RequestMapping("/sunday/web/model")
@Api(value = "model", description = "托盘模板")
public class SundayTrayModelController extends BaseController {
    @Autowired
    private SundayTrayModelService modelService;

    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model
    ) {
        return "/sunday/tray/model/index";
    }

    /**
     * 详情页
     *
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value = "id", required = true) Long id,
            Model model
    ) {
        SundayTrayModel trayModel = modelService.findOne(id);
        model.addAttribute("trayModel", trayModel);
        return "/sunday/tray/model/input";
    }


    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        PageResult<SundayTrayModel> result = modelService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }


    /**
     * 新增或修改
     *
     * @param request
     * @param response
     * @param model
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute SundayTrayModel model,
            @RequestParam(value = "imageFile", required = false) MultipartFile imageFile
    ) throws IOException {
        modelService.save(model,imageFile);
        ResponseMessage.success();
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true) Long id
    ) {
        modelService.delete(id);
        ResponseMessage.success();
    }
    @RequestMapping("/choose")
    public String choose(HttpServletRequest request,
                         @RequestParam(value = "isSingle", required = true) Integer isSingle,//是否单选
                         @RequestParam(value = "callBack", required = false) String callBack,//回调参数
                         @RequestParam(value = "param", required = false) String param,//查询参数
                         @RequestParam(value = "no", required = false) String no,//选择器id
                         Model model) {
        model.addAttribute("param", param);
        model.addAttribute("callBack", callBack);
        model.addAttribute("no", no);
        if (isSingle == null) {
            return "/sunday/tray/model/choose_erro";
        } else if (isSingle == 1) {
            return "/sunday/tray/model/choose_single";
        }
        return "/sunday/tray/model/choose_erro";
    }






}
